package com.huaxiafinance.management.controller;


import com.huaxiafinance.management.dto.request.MemberDto;
import com.huaxiafinance.management.dto.request.PassWhitelistDto;
import com.huaxiafinance.management.dto.request.QuerFroUserListDto;
import com.huaxiafinance.management.service.UserService;
import com.huaxiafinance.management.service.YfqFrontUserService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by lenovo on 2017/5/9.
 */
@Controller("yfqFrontUserController")
@RequestMapping("/froUser")
public class YfqFrontUserController {

  @Autowired
  private YfqFrontUserService yfqFrontUserService;
  @Autowired
  private UserService userService;
  private static final Logger log = Logger.getLogger(YfqFrontUserController.class);

  @RequestMapping(value = "/queryList", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String query(@ModelAttribute("dto") QuerFroUserListDto dto) throws Exception {
    MessageResult messageResult = new MessageResult();

    try {
      List dtoList = yfqFrontUserService.queryList(dto);
      messageResult.setData(dtoList);
      messageResult.setRows(yfqFrontUserService.countList(dto));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
    }

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/queryDeatil", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryDeatil(String userUuid) throws Exception {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult.setData(yfqFrontUserService.queryInformation(userUuid));
      messageResult.setSuccessResult();
    } catch (Exception e) {
      messageResult.setFailureResult();
    }

    return messageResult.getAsJSON();
  }


  @RequestMapping(value = "/blackOrWhite", produces = "application/json;charset=utf-8")
  public @ResponseBody
  String blackOrWhite(String userUuid, Integer isBlack) throws Exception {
    MessageResult messageResult = null;
    messageResult = yfqFrontUserService.blackOrWhite(userUuid, isBlack);
    messageResult.setSuccess(true);

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/passWhitelist", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String passWhitelist(@Valid @ModelAttribute("dto") PassWhitelistDto dto) throws Exception {
    log.info("鉴权通过参数" + dto);

    MessageResult messageResult = null;

    messageResult = yfqFrontUserService.passWhitelist(dto);

    log.info("鉴权通过参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求积分商城会员等级列表
   *
   * @return 会员列表
   */
  @RequestMapping(value = "/queryIntegralLevel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryIntegralLevel() {
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = yfqFrontUserService.queryScoreGrade();
    } catch (Exception e) {
      log.info("请求会员列表异常原因:" + e);
      messageResult.setFailureResult();
    }

    log.info("返回参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求会员详情以及会员积分的增删详情列表
   *
   * @param dto 请求参数
   * @return 会员详情以及积分列表增删详情列表
   */
  @RequestMapping(value = "/queryMemberDetails", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMemberDetails(@Valid @ModelAttribute("dto") MemberDto dto) {
    log.info("请求会员详情参数:" + dto);
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = yfqFrontUserService.queryMemberDetails(dto);
    } catch (Exception e) {
      log.info("请求会员详情异常原因:" + e);
      messageResult.setFailureResult();
    }
    log.info("返回会员详情参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求会员等级详情以及会员积分增加详情列表
   *
   * @param dto 请求参数
   * @return 请求会员等级详情以及会员积分增加详情列表
   */
  @RequestMapping(value = "/queryMembershipLevel", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMembershipLevel(@Valid @ModelAttribute("dto") MemberDto dto) throws Exception {
    log.info("请求会员等级详情参数:" + dto);
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = yfqFrontUserService.queryMembershipLevel(dto);
    } catch (Exception e) {
      log.info("请求会员等级详情参数:" + e);
      messageResult.setFailureResult();
    }

    log.info("请求会员等级详情参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 请求会员收货地址
   *
   * @param dto 请求参数
   * @return 收货地址列表
   */
  @RequestMapping(value = "/queryUserAddress", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryUserAddress(@Valid @ModelAttribute("dto") MemberDto dto) throws Exception {
    log.info("请求会员地址参数:" + dto);
    MessageResult messageResult = new MessageResult();

    try {
      messageResult = yfqFrontUserService.queryUserAddress(dto);
    } catch (Exception e) {
      log.info("请求会员地址参数:" + e);
      messageResult.setFailureResult();
    }

    log.info("请求会员地址参数:" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }


  @RequestMapping(value = "/queryUserImg", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryUserImg(String orderNo) throws Exception {
    log.info("查询订单号" + orderNo);
    MessageResult messageResult = new MessageResult();
    messageResult.setData(userService.queryUserImg(orderNo));
    messageResult.setSuccessResult();
    return messageResult.getAsJSON();
  }
}
